package com.yxkj.syh.app.huarong.activities.creat.add_goods;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;

import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.syh.app.basic.base.BaseActivity;
import com.syh.app.basic.bean.LiveDataEvent;
import com.syh.app.basic.bindingAdp.edittext.SimpleTextWatcher;
import com.syh.app.basic.utils.ComUtil;
import com.yxkj.syh.app.huarong.BR;
import com.yxkj.syh.app.huarong.R;
import com.yxkj.syh.app.huarong.adps.GoodsListAdp;
import com.yxkj.syh.app.huarong.bean.Goods;
import com.yxkj.syh.app.huarong.databinding.ActivityAddGoodsBinding;

import java.io.Serializable;
import java.util.List;

public class AddGoodsActivity extends BaseActivity<ActivityAddGoodsBinding, AddGoodsVM> {

    private List<String> addedCodes;
    private Goods preSelectedGoods;
    private GoodsListAdp mGoodsListAdp;

    @Override
    protected int getContentLayout() {
        return R.layout.activity_add_goods;
    }

    @Override
    protected int getViewModelId() {
        return BR.addGoodsVM;
    }

    @Override
    protected void initIntentData() {
        Intent intent = getIntent();
        addedCodes = intent.getStringArrayListExtra("added");
        preSelectedGoods = (Goods) intent.getSerializableExtra("preSelectedGoods");
    }

    @Override
    protected void initView(Bundle savedInstanceState) {

        mVDBinding.titleView.setLeftOnClickListener(v -> {
            finish();
        });

        mGoodsListAdp = new GoodsListAdp();
        mVDBinding.rvGoods.setLayoutManager(new GridLayoutManager(this, 3, RecyclerView.VERTICAL, false));
        mGoodsListAdp.setOnClick(goods -> {
            mViewModel.setSelectedGoods(goods);
        });
        mVDBinding.rvGoods.setAdapter(mGoodsListAdp);

        mVDBinding.etPack.addTextChangedListener(new SimpleTextWatcher() {
            @Override
            public void afterTextChanged(Editable s) {
                super.afterTextChanged(s);
                mVDBinding.etPack.setSelection(s.length());
            }
        });
        mVDBinding.etWeight.addTextChangedListener(new SimpleTextWatcher() {
            @Override
            public void afterTextChanged(Editable s) {
                super.afterTextChanged(s);
                mVDBinding.etWeight.setSelection(s.length());
            }
        });
    }

    @Override
    protected void initData() {
        if (preSelectedGoods != null) {
            mGoodsListAdp.setPreSelectedGoodsCode(preSelectedGoods.getCode());
            mViewModel.setSelectedGoods(preSelectedGoods);
            mViewModel.ofPack.set(preSelectedGoods.getPackages()+"");
            mViewModel.ofWeight.set(ComUtil.clearNoUseZero(preSelectedGoods.getQuantity()).toPlainString());
        }
        mViewModel.setAddedCodes(addedCodes);
        mViewModel.goodsList();
    }

    @Override
    protected void initObservers() {
        super.initObservers();

        mViewModel.mldOperate.observe(this, liveDataEvent -> {
            Intent intent = new Intent();
            Bundle bundle = new Bundle();
            bundle.putSerializable("goods", (Goods) liveDataEvent.data);
            bundle.putInt("operate", liveDataEvent.what);
            intent.putExtras(bundle);
            setResult(RESULT_OK, intent);
            finish();
        });

        mViewModel.mldGoods.observe(this, goods -> {
            mGoodsListAdp.setNewData(goods);
        });

    }

}
